/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.google.common.collect.Lists;
import com.thinkgem.jeesite.kpoint.core.base.KpointBaseEntity;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 角色Entity
 *
 * @author kongxiangsheng
 * @version 2013-12-05
 */
@Data
public class SysRole extends KpointBaseEntity {
    public static final String COL_NAME = "`name`";
    public static final String COL_IS_SYS = "`is_sys`";

    private static final long serialVersionUID = 1L;
    /**
     * 角色名称
     */
    private String name;
    /**
     * 是否是系统数据
     */
    private String isSys;

    /**
     * 原角色名称
     */
    @TableField(exist = false)
    private String oldName;
    /**
     * 拥有菜单列表
     */
    @TableField(exist = false)
    private List<SysMenu> sysMenuList = Lists.newArrayList();

    public List<String> getMenuIdList() {
        List<String> menuIdList = Lists.newArrayList();
        for (SysMenu sysMenu : sysMenuList) {
            menuIdList.add(sysMenu.getId());
        }
        return menuIdList;
    }

    public void setMenuIdList(List<String> menuIdList) {
        sysMenuList = Lists.newArrayList();
        for (String menuId : menuIdList) {
            SysMenu sysMenu = new SysMenu();
            sysMenu.setId(menuId);
            sysMenuList.add(sysMenu);
        }
    }

    public String getMenuIds() {
        return StringUtils.join(getMenuIdList(), ",");
    }

    public void setMenuIds(String menuIds) {
        sysMenuList = Lists.newArrayList();
        if (menuIds != null) {
            String[] ids = StringUtils.split(menuIds, ",");
            setMenuIdList(Lists.newArrayList(ids));
        }
    }

    /**
     * 获取权限字符串列表
     */
    public List<String> getPermissions() {
        List<String> permissions = Lists.newArrayList();
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getPermission() != null && !"".equals(sysMenu.getPermission())) {
                permissions.add(sysMenu.getPermission());
            }
        }
        return permissions;
    }

}
